home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / vol15n11.zip / JAVAP.ZIP / JAVAPP.ZIP / SIMPLE.JAV < prev    next >
Text File  |  1996-03-26  |  3KB  |  122 lines

  1. import java.io.*;
  2. import java.awt.image.*;
  3. import java.awt.*;
  4. import java.net.*;
  5.  
  6. public class Simple extends java.applet.Applet implements Runnable, ImageObserver {
  7.  
  8.     Image img;
  9.  
  10.     int xpos = 0, direction, imgWidth, imgHeight, stepSize, interval;
  11.  
  12.     Thread moveImage = null;
  13.  
  14.     // Applet functions
  15.     public void init() {
  16.         img = getImage(getCodeBase(), "pcmag.gif");
  17.  
  18.         // Typecast parameters to 0
  19.         String ap = getParameter("STEPSIZE");
  20.         stepSize = (ap != null) ? Integer.valueOf(ap).intValue() : 5;
  21.         ap = getParameter("INTERVAL");
  22.         interval = (ap != null) ? Integer.valueOf(ap).intValue() : 100;
  23.  
  24.         img.getWidth(this);
  25.         //{{INIT_CONTROLS
  26.         setLayout(null);
  27.         addNotify();
  28.         resize(insets().left + insets().right + 474, insets().top + insets().bottom + 225);
  29.         btnStart=new Button("&Start");
  30.         add(btnStart);
  31.         btnStart.reshape(insets().left + 7,insets().top + 165,77,30);
  32.         btnStop=new Button("S&top");
  33.         add(btnStop);
  34.         btnStop.reshape(insets().left + 91,insets().top + 165,77,30);
  35.         //}}
  36.  
  37.     }
  38.  
  39.     public void paint(Graphics g) {
  40.         Dimension d = size();
  41.         g.setColor(Color.gray);
  42.         g.drawRect(0, 0, d.width - 1, d.height - 1);
  43.         g.drawImage(img, xpos, 50, null);
  44.     }
  45.  
  46.     public void start() {
  47.         if (moveImage == null) {
  48.  
  49.             moveImage = new Thread(this);
  50.             moveImage.start();
  51.             direction = stepSize;
  52.  
  53.         }
  54.     }
  55.  
  56.     public void stop() {
  57.         moveImage = null;
  58.     }
  59.  
  60.     // Runnable Interface functions
  61.     public void run() {
  62.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  63.         while (true) {
  64.             try {
  65.  
  66.                 repaint();
  67.  
  68.                 Thread.sleep(interval);
  69.                 xpos += direction;
  70.  
  71.                 if (xpos > size().width - imgWidth) {
  72.                     direction = -stepSize;
  73.                     xpos = size().width - imgWidth;
  74.                 }
  75.                 else if (xpos < 0) {
  76.                     direction = stepSize;
  77.                     xpos = 0;
  78.                 }
  79.  
  80.             } catch (InterruptedException e) {
  81.  
  82.                 moveImage.destroy();
  83.                 return;
  84.  
  85.             }
  86.         }
  87.     }
  88.  
  89.     // ImageObserver Interface functions
  90.     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
  91.         imgWidth = width;
  92.         imgHeight = height;
  93.         return false;
  94.     }
  95.     //{{DECLARE_CONTROLS
  96.     Button btnStart;
  97.     Button btnStop;
  98.     //}}
  99.  
  100.     public void clickedBtnStart() {
  101.         moveImage.resume();
  102.     }
  103.  
  104.     public boolean handleEvent(Event event) {
  105.         if (event.id == Event.ACTION_EVENT && event.target == btnStop) {
  106.                 clickedBtnStop();
  107.                 return true;
  108.         }
  109.         else
  110.  
  111.         if (event.id == Event.ACTION_EVENT && event.target == btnStart) {
  112.             clickedBtnStart();
  113.             return true;
  114.         }
  115.  
  116.         return super.handleEvent(event);
  117.     }
  118.     public void clickedBtnStop() {
  119.         moveImage.suspend();
  120.     }
  121. }
  122.